package com.example.tms.controller;

import com.example.tms.bean.*;
import com.example.tms.mapper.CarMapper;
import com.example.tms.mapper.WaybillMapper;
import com.example.tms.mapper.ErrorMapper;
import com.example.tms.tools.GlobalParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报错管理
 */
@RestController
public class ErrorController {

    private final ErrorMapper errorMapper;
    private final WaybillMapper waybillMapper;
    private final CarMapper carMapper;

    @Autowired
    public ErrorController(ErrorMapper errorMapper, WaybillMapper waybillMapper, CarMapper carMapper) {
        this.errorMapper = errorMapper;
        this.waybillMapper = waybillMapper;
        this.carMapper = carMapper;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //查询报错
    @RequestMapping(value = "error/{ydh}", method = RequestMethod.GET)
    public ReturnInfo getError(@PathVariable("ydh") String ydh){
        return ReturnInfo.info2(errorMapper.findAllByYdhOrderByCjsjDesc(ydh));
    }

    //生成报错
    @RequestMapping(value = "error", method = RequestMethod.POST)
    public List<Map<String, Object>> saveError(@RequestBody ErrorInfo errorInfo) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        if(errorInfo.getYdh().equals("")){
            map.put("ydh","0");
        }else if(waybillMapper.findByYdh(errorInfo.getYdh())==null){
            map.put("ydh","1");
        }
        if(errorInfo.getSqyy().equals("")){
            map.put("sqyy","0");
        }
        list.add(map);
        if(map.isEmpty()){
            errorInfo.setSqrq(GlobalParam.getSRq());
            errorInfo.setCjsj(GlobalParam.getTime());
            errorInfo.setShjg("");
            errorInfo.setShr("");
            errorInfo.setShrq("");
            String[] a = errorInfo.getSqthzt().split("_");
            errorInfo.setSqthzt(errorInfo.getSqthzt());
            errorMapper.save(errorInfo);
            logger.info("生成报错"+errorInfo.toString());
            switch (a[0]){
                case "快捷":
                    waybillMapper.modifyWaybill(errorInfo.getYdh(),"快捷_审核中");
                    break;
                case "调度":
                    waybillMapper.modifyWaybill(errorInfo.getYdh(),"调度_审核中");
                    break;
                case "登记":
                    waybillMapper.modifyWaybill(errorInfo.getYdh(),"登记_审核中");
                    break;
                case "报账":
                    waybillMapper.modifyWaybill(errorInfo.getYdh(),"报账_审核中");
                    break;
                default:
                    break;
            }
        }
        return list;
    }

    //报错审核
    @RequestMapping(value = "error", method = RequestMethod.PUT)
    public List<Map<String, Object>> updateError(@RequestBody ErrorInfo errorInfo){
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        if(errorInfo.getBcid().equals("")){
            map.put("bcid","0");
        }else if(errorMapper.findOne(errorInfo.getBcid())==null) {
            map.put("bcid","1");
        }
        list.add(map);
        if(map.isEmpty()){
            errorMapper.bcsh(errorInfo.getBcid(),errorInfo.getShjg(),errorInfo.getShr(),GlobalParam.getSRq());
            ErrorInfo errorInfo1 = errorMapper.findOne(errorInfo.getBcid());
            String[] a  =errorInfo1.getSqthzt().split("_");
            WaybillMgmt waybillMgmt1 = waybillMapper.findByYdh(errorInfo.getYdh());
            if(errorInfo.getShjg().equals("同意")){
                if(errorInfo1.getSqthzt().equals("待调度")){
                    WaybillMgmt waybillMgmt = waybillMapper.findByYdh(errorInfo1.getYdh());
                    waybillMgmt.setYdzt(a[1]);
                    waybillMgmt.setCph("");
                    waybillMgmt.setCllx("");
                    waybillMgmt.setJsyxm("");
                    waybillMgmt.setJsysj("");
                    waybillMgmt.setBglyh("");
                    waybillMgmt.setLlcyje("");
                    waybillMgmt.setFcrq("");
                    waybillMapper.saveAndFlush(waybillMgmt);
                }else {
                  waybillMapper.modifyWaybill(errorInfo1.getYdh(),a[1]);
                  carMapper.updateCarfp(waybillMgmt1.getCph());
                }
            }else {
                waybillMapper.modifyWaybill(errorInfo1.getYdh(),errorInfo1.getYdzt());
                carMapper.updateBackCarfp(waybillMgmt1.getCph());

            }
            logger.info("报错审核"+errorInfo.toString());
        }
        return list;
    }

}